import time

import redis
pool=redis.ConnectionPool(host="192.168.100.3",port=6379,decode_responses=True)
client=redis.Redis(connection_pool=pool)

#string类型API操作
#增删改查
#插入：
client.set('key1','value1')

client.setnx('key2','value2')
client.setnx('key','value222')

#查询
value=client.get('key1')
#print(value)
print(client.get('key1'))
print(client.get('key2'))

#追加：value-add
client.append('key1','add')
print(client.get('key1'))

#查询值的长度
print(client.strlen('key1'))

#自增 incr  incrby
client.set('score',90)
#不指定自增加1,key的值不存在返回结果从一开始；
print(client.incr('score'))
print(client.incrby('score',5))

#自减  decr

#批量插入
client.mset(k1="v1",k2="v2")

#批量查询
print(client.mget('k1','k2'))


client.mset(k3="abcdefg")
#范围查找
print(client.getrange("k3",0,2))
#更新操作
client.setrange("k3",0,"aaa")
print(client.getrange("k3",0,-1))

#设置key的同时来设置有效时间
client.setex("key4","value4",5)
print(client.ttl("key4"))
time.sleep(1)
print(client.ttl("key4"))
time.sleep(1)
print(client.ttl("key4"))

client.mset(k5="value5")
print(client.getset("k5","v5"))
print(client.get("k5"))

